@extends('layouts.admin')

@section('title', '商品推送')

@section('content')
    <div class="console-title">
        <div class="float-right">
            <form method="get" id="searchForm" action="">
                <label>
                    <select title="" name="groupid" class="form-control">
                        @foreach ($grouplist as $group)
                            <option value="{{$group['groupid']}}"@if($groupid==$group['groupid']) selected="selected"@endif>{{$group['grouptitle']}}</option>
                        @endforeach
                    </select>
                </label>
                <label>
                    <button type="submit" class="btn btn-primary">搜索</button>
                </label>
                <label>
                    <a href="{{admin_url('itempush')}}">
                        <span class="btn btn-primary">返回分组</span>
                    </a>
                </label>
            </form>
        </div>
        <h2>商品管理->商品推送</h2>
    </div>

    <div class="content-div" style="border: 1px #CCC solid;">
        <form method="post" id="listForm" autocomplete="off">
            @csrf
            <table cellpadding="0" cellspacing="0" border="0" width="100%" class="dsxui-listtable border-0">
                <thead>
                <tr>
                    <th width="40">删?</th>
                    <th width="80">图片</th>
                    <th>宝贝名称</th>
                    <th width="150">分组</th>
                    <th width="80">价格</th>
                    <th width="80">销量</th>
                    <th width="80">状态</th>
                    <th width="140">创建时间</th>
                    <th width="60">选项</th>
                </tr>
                </thead>
            </table>
            <div class="sortable">
                @foreach ($items as $item)
                    <table cellpadding="0" cellspacing="0" border="0" width="100%" class="dsxui-listtable border-0">
                        <tbody>
                        <tr>
                            <td width="40">
                                <input title="" type="checkbox" class="checkmark" name="delete[]" value="{{$item['id']}}">
                                <input type="hidden" name="items[]" value="{{$item['id']}}">
                            </td>
                            <td width="80"><div class="bg-cover" rel="setImage" data-id="{{$item['id']}}" style="background-image: url({{image_url($item['image'])}}); width: 80px; height: 80px;"></div></td>
                            <td><h3 class="title"><a href="{{item_url($item['itemid'])}}" target="_blank">{{$item['title']}}</a></h3></td>
                            <td width="150">{{$item['grouptitle']}}</td>
                            <td width="80"><p><strong style="color: #f40;">{{$item['item']['price']}}</strong></p></td>
                            <td width="80">{{$item['item']['sold']}}</td>
                            <td width="80">
                                @if ($item['item']['on_sale'])
                                    出售中
                                @else
                                    已下架
                                @endif
                            </td>
                            <td width="140">{{@date('Y-m-d H:i', $item['item']['created_at'])}}</td>
                            <td width="60"><a rel="edit" data-id="{{$item['id']}}" data-itemid="{{$item['itemid']}}" data-title="{{$item['title']}}">编辑</a></td>
                        </tr>
                        </tbody>
                    </table>
                @endforeach
            </div>
            <table cellpadding="0" cellspacing="0" border="0" width="100%" class="dsxui-listtable border-0">
                <tfoot>
                <tr>
                    <td colspan="20" class="btn-group-sm">
                        <div class="float-right">{!! $pagination !!}</div>
                        <label><input type="checkbox" data-action="checkall"> 全选</label>
                        <label><button type="button" class="btn btn-primary" id="submitButton">提交</button></label>
                        <label><button type="button" class="btn btn-default" id="addButton">添加宝贝</button></label>
                        <label><button type="button" class="btn btn-default" data-action="refresh">刷新</button></label>
                    </td>
                </tr>
                </tfoot>
            </table>
        </form>
    </div>
    <script type="text/html" id="newItemTpl">
        <form method="post" id="formNewItem" action="{{admin_url('itempush/add')}}">
            @csrf
            <input type="hidden" name="id" value="{id}">
            <input type="hidden" name="groupid" value="{{$groupid}}">
            <table width="100%" class="formtable">
                <tbody>
                <tr>
                    <td width="60">商品ID</td>
                    <td width="300"><input title="" type="text" class="form-control" id="itemid" name="itemid" value="{itemid}"></td>
                    <td></td>
                </tr>
                <tr>
                    <td>标题</td>
                    <td><input title="" type="text" class="form-control" id="title" name="title" value="{title}"></td>
                    <td class="cell-tips">不填写将自动获取</td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <button type="button" id="submitNewItem" class="btn btn-primary">提交</button>
                    </td>
                </tr>
                </tbody>
            </table>
        </form>
    </script>
    <script type="text/javascript">
        $(function () {
            $(".sortable").sortable();
            $(document).on('click', function () {
                if ($(".checkmark:checked").length > 0) {
                    $("#deleteButton").enable();
                } else {
                    $("#deleteButton").disable();
                }
            });

            $("#submitButton").on('click', function () {
                if ($(".checkmark:checked").length > 0)
                {
                    DSXUI.showConfirm('确认要删除所选商品吗?', function () {
                        $("#listForm").submit();
                    });
                } else {
                    $("#listForm").submit();
                }
            });

            var showDialog = function(id, itemid, title) {
                if (!id) id = 0;
                if (!itemid) itemid = '';
                if (!title) title = '';
                var content = $("#newItemTpl").html().replace(/\{id\}/, id)
                    .replace(/\{itemid\}/, itemid).replace(/\{title\}/, title);
                DSXUI.dialog({
                    title:'添加推荐商品',
                    content:content,
                    showFooter:false,
                    style:{
                        width:'500px'
                    },
                    afterShow:function (dlg) {
                        $("#submitNewItem").on('click', function () {
                            var itemid = $("#itemid").val();
                            if (!itemid) {
                                DSXUI.error('请填写商品ID');
                                return false;
                            }
                            $("#formNewItem").ajaxSubmit({
                                beforeSend:dlg.close,
                                success:function (response) {
                                    DSXUtil.reFresh();
                                }
                            });
                        });
                    }
                });
            };
            $("#addButton").on('click', function () {
                showDialog();
            });
            $("[rel=edit]").on('click', function () {
                var id = $(this).attr('data-id');
                var itemid = $(this).attr('data-itemid');
                var title = $(this).attr('data-title');
                showDialog(id, itemid, title);
            });

            $("[rel=setImage]").on('click', function () {
                var self = $(this);
                DSXUI.showImagePicker(function (data) {
                    self.css('background-image', 'url('+data.imageurl+')');
                    $.post('{{admin_url('itempush/setimage')}}', {id:self.attr('data-id'), image:data.image});
                });
            });
        });
    </script>
@stop
